﻿// -----------------------------------------------------------------------
//  <copyright file="IRoleSyncService.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-03-26 14:03</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Domain.Events;
using DaprPlus.Extensions;
using DaprPlus.Identity.Domain.Entities;
using DaprPlus.Identity.Domain.IntegrationEvents;
using DaprPlus.Specifications;
using Microsoft.Extensions.DependencyInjection;

namespace DaprPlus.Identity.Domain.Services;

public interface IRoleSyncService
{
    Task Sync();
}

public class RoleSyncService(IServiceProvider provider) : IRoleSyncService
{
    public async Task Sync()
    {
        var repository = provider.GetRepository<Role>();
        var trueSpec = new TrueSpecification<Role>();
        var dtos = await repository.GetAllAsync<RoleInRecord>(trueSpec);
        var syncEvent = new RoleSyncIntegrationEvent([.. dtos], true);
        var eventBus = provider.GetRequiredService<IIntegrationEventBus>();
        await eventBus.PublishAsync(syncEvent);
    }
}
